package cn.edu.xmu.restfuldemo.service.mq;

import cn.edu.xmu.restfuldemo.dao.OrderItemDao;
import cn.edu.xmu.restfuldemo.dao.OrdersDao;
import cn.edu.xmu.restfuldemo.model.BO.OrderItem;
import cn.edu.xmu.restfuldemo.model.VO.OrdersRetVo;
import cn.edu.xmu.restfuldemo.util.JacksonUtil;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author RenJieZheng
 */
@Service
@RocketMQMessageListener(topic = "orders-orderitem-topic", consumeMode = ConsumeMode.CONCURRENTLY, consumeThreadMax = 10, consumerGroup = "order-group")
public class OrderConsumerListener implements RocketMQListener<String> {
    @Autowired
    OrdersDao ordersDao;

    @Autowired
    OrderItemDao orderItemDao;

    @Override
    public void onMessage(String s) {
        OrdersRetVo ordersRetVo = JacksonUtil.toObj(s,OrdersRetVo.class);
        ReturnObject<Long> returnObject = ordersDao.addOrder(ordersRetVo.newOrdersFromOrdersRetVo());
        Long orderId = returnObject.getData();
        for(OrderItem orderItem:ordersRetVo.newOrderItemList()){
            orderItem.setOrderId(orderId);
            orderItemDao.addOrderItem(orderItem);
        }
    }
}
